package com.niiwoo.civet.mobile.controller.invest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.mobile.vo.request.invest.DayInvestInfoRequestVO;
import com.niiwoo.civet.mobile.vo.request.invest.InvestCalendarDayStatRequestVO;
import com.niiwoo.civet.mobile.vo.request.invest.InvestCalendarMonthStatRequestVO;
import com.niiwoo.civet.mobile.vo.request.invest.MonthEveryDayInvestStatusRequestVO;
import com.niiwoo.civet.mobile.vo.response.invest.DayInvestInfoResponseVO;
import com.niiwoo.civet.mobile.vo.response.invest.DayInvestOrderInfoVO;
import com.niiwoo.civet.mobile.vo.response.invest.DayInvestorPlanInfoVO;
import com.niiwoo.civet.mobile.vo.response.invest.InvestCalendarDayStatResponseVO;
import com.niiwoo.civet.mobile.vo.response.invest.InvestCalendarMonthStatResponseVO;
import com.niiwoo.civet.mobile.vo.response.invest.MonthEveryDayInvestStatusResponseVO;
import com.niiwoo.civet.trade.dto.request.invest.InvestCalendarRequestDTO;
import com.niiwoo.civet.trade.dto.response.invest.DayInvestInfoResponseDTO;
import com.niiwoo.civet.trade.dto.response.invest.DayInvestOrderInfoDTO;
import com.niiwoo.civet.trade.dto.response.invest.DayInvestorPlanInfoDTO;
import com.niiwoo.civet.trade.dto.response.invest.DayPackageInfoDTO;
import com.niiwoo.civet.trade.dto.response.invest.InvestCalendarDayStatResponseDTO;
import com.niiwoo.civet.trade.dto.response.invest.InvestCalendarMonthStatResponseDTO;
import com.niiwoo.civet.trade.dto.response.invest.MonthEveryDayInvestStatusDTO;
import com.niiwoo.civet.trade.service.invest.InvestCalendarDubboService;
import com.niiwoo.tripod.base.utils.DateUtils;
import com.niiwoo.tripod.web.util.PrincipalContext;
import com.niiwoo.tripod.web.vo.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/investCalendar")
@Api(tags = "投资日历(周林泉)", description = "/investCalendar")
public class InvestCalendarController {
	
	@Reference(version="1.0.0")
	private InvestCalendarDubboService  investCalendarDubboService;
	
	@PostMapping("/getMonthEveryDayInvestStatus")
    @ApiOperation("V5.5.5 获取指定月份每天的投资回款状态(周林泉)")
    public Result<List<MonthEveryDayInvestStatusResponseVO>> getMonthEveryDayInvestStatus(@RequestBody MonthEveryDayInvestStatusRequestVO request) throws Exception {
		String userId = String.valueOf(PrincipalContext.getUserId());
		
		DateTime monthFirstDay = new DateTime();
		monthFirstDay = monthFirstDay.withYear(request.getYear()).withMonthOfYear(request.getMonth()).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime nextMonthFirstDay = monthFirstDay.plusMonths(1);
		
		//参数： userId/monthFirstDay/nextMonthFirstDay
		InvestCalendarRequestDTO requestDTO = new InvestCalendarRequestDTO();
		requestDTO.setUserId(userId);
		requestDTO.setBeginDate(monthFirstDay.toDate());
		requestDTO.setEndDate(nextMonthFirstDay.toDate());
		
		List<MonthEveryDayInvestStatusResponseVO> result = new ArrayList<>();
		List<MonthEveryDayInvestStatusDTO> list = investCalendarDubboService.getMonthEveryDayInvestStatus(requestDTO);
		if (list == null || list.isEmpty()) return Result.with(result);
		
		MonthEveryDayInvestStatusResponseVO vo = null;
		for (MonthEveryDayInvestStatusDTO dto : list) {
			vo = new MonthEveryDayInvestStatusResponseVO();
			
			vo.setReturnedMoneyDate(dto.getReturnedMoneyDate());
			vo.setStatus(dto.getStatus());
			result.add(vo);
		}
		
        return Result.with(result);
    }
	
	@PostMapping("/getDayInvestInfo")
    @ApiOperation("V5.5.5 获取指定日期投资回款信息(周林泉)")
    public Result<DayInvestInfoResponseVO> getDayInvestInfo(@RequestBody DayInvestInfoRequestVO request) throws Exception {
		String userId = String.valueOf(PrincipalContext.getUserId());
			
		Date date = DateUtils.parseDateFromString(request.getDate(), DateUtils.SHOW_DATE_FORMAT);
		DateTime nextDateTime = new DateTime(date.getTime());
		Date nextDate = nextDateTime.plusDays(1).toDate();
		
		//参数： userId/date/nextDate
		InvestCalendarRequestDTO requestDTO = new InvestCalendarRequestDTO();
		requestDTO.setUserId(userId);
		requestDTO.setBeginDate(date);
		requestDTO.setEndDate(nextDate);
		
		DayInvestInfoResponseDTO responseDTO = investCalendarDubboService.getDayInvestInfo(requestDTO);
		
		DayInvestInfoResponseVO result = new DayInvestInfoResponseVO();
		if (responseDTO == null) return Result.with(result);
		
		result.setNum(responseDTO.getNum());
		result.setReceivedAmount(responseDTO.getReceivedAmount());
		result.setUnreceivedAmount(responseDTO.getUnreceivedAmount());
		result.setReturnedMoneyDate(result.getReturnedMoneyDate());
		
		List<DayInvestOrderInfoVO> creditList = new ArrayList<>();
		List<DayInvestOrderInfoVO> debtList = new ArrayList<>();
		//List<DayInvestOrderInfoVO> mortgageList = new ArrayList<>();
		List<DayInvestOrderInfoVO> packageList = new ArrayList<>();
		
		//信用标列表
		if (responseDTO.getCreditsLoans() != null && !responseDTO.getCreditsLoans().isEmpty()) {
			for (DayInvestOrderInfoDTO item : responseDTO.getCreditsLoans()) {
				creditList.add(parseDayInvestOrderInfoVO(item));
			}
			result.setCreditsLoans(creditList);
		}
		//债转标列表
		if (responseDTO.getDebtOrders() != null && !responseDTO.getDebtOrders().isEmpty()) {
			for (DayInvestOrderInfoDTO item : responseDTO.getDebtOrders()) {
				debtList.add(parseDayInvestOrderInfoVO(item));
			}
			result.setDebtOrders(debtList);
		}
		//抵押标列表
		//if (responseDTO.getMortgageLoans() != null && !responseDTO.getMortgageLoans().isEmpty()) {
		//	for (DayInvestOrderInfoDTO item : responseDTO.getMortgageLoans()) {
		//		mortgageList.add(parseDayInvestOrderInfoVO(item));
		//	}
		//	result.setMortgageLoans(mortgageList);
		//}
		//智能投列表
		if (responseDTO.getPackageOrders() != null && !responseDTO.getPackageOrders().isEmpty()) {
			for (DayPackageInfoDTO item : responseDTO.getPackageOrders()) {
				packageList.add(parseDayPackageInfoVO(item));
			}
			result.setPackageOrders(packageList);
		}
		
        return Result.with(result);
    }
	
	private DayInvestOrderInfoVO parseDayPackageInfoVO(DayPackageInfoDTO item) {
		DayInvestOrderInfoVO vo = new DayInvestOrderInfoVO();
		
		vo.setInvestOrderId(item.getPackageOrderId());
		
		if (item.getInvestorPlans() != null && !item.getInvestorPlans().isEmpty()) {
			List<DayInvestorPlanInfoVO> subList = new ArrayList<>();
			
			DayInvestorPlanInfoVO subVO = null;
			for (DayInvestorPlanInfoDTO subItem : item.getInvestorPlans()) {
				subVO = new DayInvestorPlanInfoVO();
				subVO.setAdvanceOverdueDays(subItem.getAdvanceOverdueDays());
				subVO.setCurrentPeriod(subItem.getCurrentPeriod());
				subVO.setDeadlineUnit(subItem.getDeadlineUnit());
				subVO.setProjectTitle(subItem.getProjectTitile());
				subVO.setStatus(subItem.getStatus());
				subVO.setBorrowRate(subItem.getBorrowRate());
				subVO.setTotalPeriod(subItem.getTotalPeriod());
				subVO.setUnreceivedAmount(subItem.getUnreceivedAmount());
				subVO.setProjectId(subItem.getProjectId());
				subVO.setInvestOrderId(subItem.getInvestOrderId());
				subVO.setIsGuarantee(subItem.getIsGuarantee());
				
				subList.add(subVO);
			}
			vo.setInvestorPlans(subList);
		}
		
		return vo;
	}
	
	private DayInvestOrderInfoVO parseDayInvestOrderInfoVO(DayInvestOrderInfoDTO item) {
		DayInvestOrderInfoVO vo = new DayInvestOrderInfoVO();
		
		vo.setInvestOrderId(item.getInvestOrderId());
		
		if (item.getInvestorPlans() != null && !item.getInvestorPlans().isEmpty()) {
			List<DayInvestorPlanInfoVO> subList = new ArrayList<>();
			
			DayInvestorPlanInfoVO subVO = null;
			for (DayInvestorPlanInfoDTO subItem : item.getInvestorPlans()) {
				subVO = new DayInvestorPlanInfoVO();
				subVO.setAdvanceOverdueDays(subItem.getAdvanceOverdueDays());
				subVO.setCurrentPeriod(subItem.getCurrentPeriod());
				subVO.setDeadlineUnit(subItem.getDeadlineUnit());
				subVO.setProjectTitle(subItem.getProjectTitile());
				subVO.setStatus(subItem.getStatus());
				subVO.setBorrowRate(subItem.getBorrowRate());
				subVO.setTotalPeriod(subItem.getTotalPeriod());
				subVO.setUnreceivedAmount(subItem.getUnreceivedAmount());
				subVO.setProjectId(subItem.getProjectId());
				subVO.setInvestOrderId(subItem.getInvestOrderId());
				subVO.setIsGuarantee(subItem.getIsGuarantee());
				
				subList.add(subVO);
			}
			vo.setInvestorPlans(subList);
		}
		
		return vo;
	}
	
	@PostMapping("/monthStat")
    @ApiOperation("V5.5.5 月统计(周林泉)")
    public Result<List<InvestCalendarMonthStatResponseVO>> monthStat(@RequestBody InvestCalendarMonthStatRequestVO request) throws Exception {
		String userId = String.valueOf(PrincipalContext.getUserId());
		
		DateTime yearFirstDay = new DateTime();
		yearFirstDay = yearFirstDay.withYear(request.getYear()).withMonthOfYear(1).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime nextYearFirstDay = yearFirstDay.plusYears(1);
		
		//参数： userId/yearFirstDay/nextYearFirstDay
		InvestCalendarRequestDTO requestDTO = new InvestCalendarRequestDTO();
		requestDTO.setUserId(userId);
		requestDTO.setBeginDate(yearFirstDay.toDate());
		requestDTO.setEndDate(nextYearFirstDay.toDate());
		
		List<InvestCalendarMonthStatResponseDTO> list = investCalendarDubboService.monthStat(requestDTO);
		
		List<InvestCalendarMonthStatResponseVO> result = new ArrayList<>();
		if (list == null || list.isEmpty()) return Result.with(result);
		
		InvestCalendarMonthStatResponseVO vo = null;
		for (InvestCalendarMonthStatResponseDTO dto : list) {
			vo = new InvestCalendarMonthStatResponseVO();
			
			vo.setMonth(dto.getMonth());
			vo.setYear(dto.getYear());
			vo.setReceivedAmount(dto.getReceivedAmount());
			vo.setReceivedInterest(dto.getReceivedInterest());
			vo.setReceivedPrincipal(dto.getReceivedPrincipal());
			vo.setUnreceivedAmount(dto.getUnreceivedAmount());
			vo.setUnreceivedInterest(dto.getUnreceivedInterest());
			vo.setUnreceivedPrincipal(dto.getUnreceivedPrincipal());
			
			result.add(vo);
		}
		
		return Result.with(result);
    }
	
	@PostMapping("/dayStat")
    @ApiOperation("V5.5.5 日统计(周林泉)")
    public Result<List<InvestCalendarDayStatResponseVO>> dayStat(@RequestBody InvestCalendarDayStatRequestVO request) throws Exception {
		String userId = String.valueOf(PrincipalContext.getUserId());
		
		DateTime monthFirstDay = new DateTime();
		monthFirstDay = monthFirstDay.withYear(request.getYear()).withMonthOfYear(request.getMonth()).withDayOfMonth(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime nextMonthFirstDay = monthFirstDay.plusMonths(1);
		
		//参数： userId/monthFirstDay/nextMonthFirstDay
		InvestCalendarRequestDTO requestDTO = new InvestCalendarRequestDTO();
		requestDTO.setUserId(userId);
		requestDTO.setBeginDate(monthFirstDay.toDate());
		requestDTO.setEndDate(nextMonthFirstDay.toDate());
		
		List<InvestCalendarDayStatResponseDTO> list = investCalendarDubboService.dayStat(requestDTO);
		
		List<InvestCalendarDayStatResponseVO> result = new ArrayList<>();
		if (list == null || list.isEmpty()) return Result.with(result);
		
		InvestCalendarDayStatResponseVO vo = null;
		for (InvestCalendarDayStatResponseDTO dto : list) {
			vo = new InvestCalendarDayStatResponseVO();
			
			vo.setMonth(dto.getMonth());
			vo.setYear(dto.getYear());
			vo.setDay(dto.getDay());
			vo.setReceivedAmount(dto.getReceivedAmount());
			vo.setReceivedInterest(dto.getReceivedInterest());
			vo.setReceivedPrincipal(dto.getReceivedPrincipal());
			vo.setUnreceivedAmount(dto.getUnreceivedAmount());
			vo.setUnreceivedInterest(dto.getUnreceivedInterest());
			vo.setUnreceivedPrincipal(dto.getUnreceivedPrincipal());
			
			result.add(vo);
		}
		
		return Result.with(result);
    }
}
